package com.zxl.always.on.display.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wx.wheelview.adapter.ArrayWheelAdapter;
import com.wx.wheelview.common.WheelConstants;
import com.wx.wheelview.util.WheelUtils;

/**
 * Created by Administrator on 2018/3/9.
 */

public class WheelAdapter extends ArrayWheelAdapter {

    private int gravity;

    private Context mContext;

    public WheelAdapter(Context context, int gravity) {
        super(context);
        this.gravity = gravity;
        mContext = context;
    }

    @Override
    public View bindView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = new WheelItem(mContext, gravity);
        }
        WheelItem item = (WheelItem) convertView;
        item.setText(mList.get(position));
        return convertView;
    }

    public class WheelItem extends FrameLayout {

        private ImageView mImage;
        private TextView mText;

        public WheelItem(Context context, int gravity) {
            super(context);
            init(gravity);
        }

        public WheelItem(Context context, AttributeSet attrs) {
            super(context, attrs);
            init(-1);
        }

        public WheelItem(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            init(-1);
        }

        /**
         * 初始化
         */
        private void init(int gravity) {
            LinearLayout layout = new LinearLayout(getContext());
            LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, WheelUtils.dip2px(getContext(), WheelConstants.WHEEL_ITEM_HEIGHT));
            layout.setOrientation(LinearLayout.HORIZONTAL);
//            layout.setPadding(WheelConstants.WHEEL_ITEM_PADDING, WheelConstants.WHEEL_ITEM_PADDING, WheelConstants
//                    .WHEEL_ITEM_PADDING, WheelConstants.WHEEL_ITEM_PADDING);
            if (gravity == -1) {
                layout.setGravity(Gravity.CENTER);
            } else {
                layout.setGravity(gravity);
            }
            addView(layout, layoutParams);

            // 图片
            mImage = new ImageView(getContext());
            mImage.setTag(WheelConstants.WHEEL_ITEM_IMAGE_TAG);
            mImage.setVisibility(View.GONE);
            LayoutParams imageParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            imageParams.rightMargin = WheelConstants.WHEEL_ITEM_MARGIN;
            layout.addView(mImage, imageParams);

            // 文本
            mText = new TextView(getContext());
            mText.setTag(WheelConstants.WHEEL_ITEM_TEXT_TAG);
            mText.setEllipsize(TextUtils.TruncateAt.END);
            mText.setSingleLine();
            mText.setIncludeFontPadding(false);
            if (gravity == -1) {
                mText.setGravity(Gravity.CENTER);
            } else {
                mText.setGravity(gravity);
            }
//            mText.setGravity(Gravity.CENTER);
            mText.setTextColor(Color.BLACK);
            LayoutParams textParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
            layout.addView(mText, textParams);
        }

        /**
         * 设置文本
         *
         * @param text
         */
        public void setText(CharSequence text) {
            mText.setText(text);
        }

        /**
         * 设置图片资源
         *
         * @param resId
         */
        public void setImage(int resId) {
            mImage.setVisibility(View.VISIBLE);
            mImage.setImageResource(resId);
        }


    }


}
